<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>质检表显示页面</title>
<link rel="stylesheet" href="../../layui/css/layui.css" media="all">
<style type="text/css">
	body{
		margin:20px 0px;
	}
	.number{
		border:none;padding-bottom:8px;padding-left:8px;
	}
</style>

</head>
<body>
	<div class="demoTable">
	  搜索:
	  <div class="layui-inline">
	    <input class="layui-input" name="qtPerson" id="demoReload" autocomplete="off" placeholder="输入关键字(质检人)">
	  </div>
		  <button class="layui-btn" data-type="reload">搜索</button>
		  <button class="layui-btn" id="tj" data-type="tj">提交</button>
	</div>
	<table class="layui-hide" id="qualityTestingModel" lay-filter="demo" name="dd"></table>
 
<script type="text/html" id="barDemo">
  	{{# if(d.qtRate == 0){ }} 
		<input class="layui-input number" qtId="{{d.qtId}}" qtSumnum="{{d.qtSumnum}}" id="nopassNum" autocomplete="off" placeholder="输入未通过数量" onkeyup="yz(this);" value="{{d.nopassNum}}" maxlength="10">
	{{# }else{ }}
		<input class="layui-input number" autocomplete="off" placeholder="输入未通过数量" value="{{d.nopassNum}}" id="num"  qtId="{{d.qtId}}" qtSumnum="{{d.qtSumnum}}">
	{{# }}}
</script>
<script type="text/javascript" src="../../res/js/base.js"></script>
<script type="text/javascript" src="../../res/mln/getUrlParam.js"></script>
<script src="../../layui/layui.js"></script>

<script>
var jsonObject = "";
layui.use(['laydate', 'laypage', 'layer', 'table', 'upload', 'element'], function(){
	  var laydate = layui.laydate //日期
	  ,laypage = layui.laypage //分页
	  layer = layui.layer //弹层
	  ,table = layui.table //表格
	  ,upload = layui.upload //上传
	  ,element = layui.element; //元素操作
	 
	 
	  //监听Tab切换
	  element.on('tab(demo)', function(data){
	    layer.msg('切换了：'+ this.innerHTML);
	    console.log(data);
	  });
	  
	  var $ = layui.$;
	  
	  //执行一个 table 实例
	  table.render({
	     elem: '#qualityTestingModel'
	    ,width:930
	    ,height: 500
	    ,url: getRootPath()+'qualityTestingModel/findAllQualityTestingModel?sumId='+getUrlParam("sumId") //数据接口
	    ,page: true //开启分页
	    ,method:'post'
	    ,cols: [[ //表头
	       {type:'checkbox', fixed: 'left'}
	      ,{field: 'qtId', title: '质检ID', width:120, fixed: 'left'}
	      ,{field: 'sumId', title: '质检总报表ID', width:120}
	      ,{field: 'nopassNum', title: '未通过数量', width:144,templet:"#barDemo"}
	      ,{field: 'qtSumnum', title: '质检总数量', width:120}
	      ,{field: 'qtRate', title: '质检率', width: 120}
	      ,{field: 'qtTimes', title: '质检时间', width: 120}
	      ,{field: 'isva', title: '是否有效', width: 120}
	  <!--,{field: 'qtPerson', title: '质检人', width: 120}-->
	  <!--,{field: 'qtRemark', title: '质检备注', width: 120}-->
	  <!--,{field: 'sort', title: '排序', width: 120}-->
	  <!--,{field: 'pwId', title: '业务ID', width: 120}-->
	      
	    ]]
	  	,id: 'testReload'
	  	,done:function(res, curr, count){
	  		jsonObject = table.cache.testReload;
	  		
	  		var sumNopass = getUrlParam("sumNopass");
	  		if(sumNopass != 0){
	  			$(".number").attr("disabled","true");
	  		}
	  		
	  		 $(".number").blur(function(event){
	 			var url = getRootPath()+"qualityTestingModel/update.action";
	 			var qtId = $(event.target).attr("qtId");
	 			var qtSumnum = $(event.target).attr("qtSumnum");
	 			var nopassNum = $(event.target).val();
	 			
	 			if(parseInt(nopassNum) > parseInt(qtSumnum)){
	 				$(event.target).val("0");
	 				layer.msg('未通过数量不能大于总数量');
	 			}else{
	 				var data = {"qtId":qtId,"qtSumnum":qtSumnum,"nopassNum":nopassNum};
		 			$.ajax({
		 				url:url,
		 				data:data,
		 				success:function(msg){
		 					
		 					if(msg=="ok"){
		 						
		 						$(".layui-laypage-skip .layui-laypage-btn").click(); //刷新当前页
		 					}
		 					
		 				}
		 			});	
	 			}
	 		});
	  	 }
	  });
	  
	  
	  //监听工具条
	  table.on('tool(demo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	    var data = obj.data //获得当前行数据
	    ,layEvent = obj.event; //获得 lay-event 对应的值
	    if(layEvent === 'del'){
	      var manId=data.manId;
	      layer.confirm('确认删除？', function(index){
	        /* obj.del(); */ //删除对应行（tr）的DOM结构
	        layer.close(index);
	        //使用jquery的ajax方式向后台提交数据
			var url = "../../qualityTestingModel/deletes";
			var data={manId:manId};
				$.post(url,data,function(data){
					if(data=="ok"){
						layer.msg('删除成功', {
							  icon: 1,
							  time: 1000 //1秒关闭（如果不配置，默认是3秒）
							}, function(){
								 location.reload(); // 页面刷新
						});
					}
					
				});
	      	});
	    }
	  });
	  
	
	  
	  
	  
	  
	  //条件查询
	 
	  
	  $('#zj').on('click', function(){
		  var arr = "";
		  var str = "";
		  for(var i=0;i<jsonObject.length;i++){
			  if(i<jsonObject.length-1){
				  str += '{"qtId":"'+jsonObject[i].qtId+'","nopassNum":"'+jsonObject[i].nopassNum+'","qtSumnum":"'+jsonObject[i].qtSumnum+'"},'
			  }else {
				  str += '{"qtId":"'+jsonObject[i].qtId+'","nopassNum":"'+jsonObject[i].nopassNum+'","qtSumnum":"'+jsonObject[i].qtSumnum+'"}'
			  }
		  }
		  arr = "["+str+"]";
		 
		  var url = getRootPath()+"qualityTestingModel/update.action";
		  var data = {"str":arr}
		  $.post(url,data,function(msg){
				if(msg =="ok"){
					layer.msg('质检成功', {
						  icon: 1,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
							 location.reload(); // 页面刷新
					});
				}else {
					layer.msg('质检失败',{
							icon:2,
							time: 1000
						},function(){
							location.reload();
					})
				}
				 
			}); 	
		
		  
	  });
	  
	  $('#tj').on('click', function(){
		  var jsonObject = table.cache.testReload;
		  var sumNopassNum = 0;
		  var sumQtSumnum = 0;
		  for(var i=0;i<jsonObject.length;i++){
			  sumNopassNum = (sumNopassNum+0)+(jsonObject[i].nopassNum+0);
			  sumQtSumnum = (sumQtSumnum+0)+(jsonObject[i].qtSumnum+0);
		  }
		  alert(sumNopassNum);
		  var url = getRootPath()+"qualityTestingModel/updateTwo.action";
		  var data = {"sumId":getUrlParam("sumId"),"sumNopassNum":sumNopassNum,"sumQtSumnum":sumQtSumnum};
		  $.ajax({
			  url:url,
			  data:data,
			  success:function(msg){
				 
				  if(msg == "ok"){
					  parent.location.reload();
				  }
			  }
		  });
		  
	  });

	  $('#qualityTestingModelAdd').on('click', function(){
		 layer.open({
			  type: 	2,
			  area : ['60%' , '80%'],//弹出框大小
			  shift:1,//弹出框动画效果
			  content: ['QualityTestingModelAdd.html','on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			}); 
	   });
});  
	 
function yz(obj){
	obj.value = obj.value.replace(/[^\d]/g,"");
	if(obj.value.indexOf(".")<0 && obj.value != ""){
		obj.value = parseFloat(obj.value);
	}
	if(obj.value.indexOf(".")==0){
		obj.value = "";
	}
}	  
	  
</script>
</body>
</html>